package example.demo.forkjoin;

import java.util.List;
import java.util.concurrent.RecursiveAction;

public class VoidTask extends RecursiveAction {
    private final int startValue;
    private final int endValue;
    private final List<?> list;
    private static final int MAX = 1;

    /*数据源*/
    private static List<?> src;
    /*目标源*/
    private static List<?> desc;

    public static void setSrc(List<?> src) {
        VoidTask.src = src;
    }

    public static void setDesc(List<?> desc) {
        VoidTask.desc = desc;
    }

    public static List<?> getDesc() {
        return desc;
    }

    public static List<?> getSrc() {
        return src;
    }

    public VoidTask(int startValue, int endValue, List<?> list) {
        this.startValue = startValue;
        this.endValue = endValue;
        this.list = list;
    }

    @Override
    protected void compute() {
        if (endValue - startValue <= MAX) {
            /*线程执行*/
            for (int i = startValue; i <= endValue; i++) {
                System.out.println(Thread.currentThread().getName() + "----" + list.get(i));
            }
        } else {
            int mid = (startValue + this.endValue) / 2;
            VoidTask subTask1 = new VoidTask(startValue, mid, list);
            subTask1.fork();
            VoidTask subTask2 = new VoidTask(mid + 1, endValue, list);
            subTask2.fork();
            //等待执行结果
            subTask1.join();
            subTask2.join();
        }
    }
}
